﻿(function ($)
{
    $.elements = {};

    $.elements.init = function (params)
    {
        $.elements.deck = params.deck[0];

        $.elements.hand = params.hand[0];

        $($.elements.deck).droppable({ drop: $.elements.deck_drop, accept: ".element" }).disableSelection();

        $('body').droppable({ drop: $.elements.body_drop, accept: ".element" });
    }

    $.elements.loadElements = function (mode)
    {
        $.post("./bus/load_elements.php", mode ? "mode=" + mode : null, function (data)
        {
            for (var item in data.Elements)
                $.elements.createHandElement(data.Elements[item].Id, data.Elements[item].Name);
        });
    }

    $.elements.removeElements = function (mode)
    {
        $.post("./bus/load_elements.php", mode ? "mode=" + mode : null, function (data)
        {
            for (var item in data.Elements)
            {
                $('div.element[elementId=' + data.Elements[item].Id + ']').remove();
            }
        });
    }

    $.elements.combineElements = function ($element, $target, ui, newElements)
    {
        if ($element.parent().attr("id") === $.elements.deck.id)
            $element.remove();

        $target.remove();

        var newElement;

        for (var item in newElements)
        {
            newElement = newElements[item];

            var $onHand = $("div.element[elementId=" + newElement.Id + "]", $.elements.hand);

            if ($onHand.length === 0)
                $onHand = $.elements.createHandElement(newElement.Id, newElement.Name);

            $.elements.createDeckElement($onHand, ui);
        }
    }

    $.elements.createDeckElement = function ($handElement, ui)
    {
        $handElement.clone().
		        css("position", "fixed").
		        css("top", ui.position.top).
		        css("left", ui.position.left).
		        attr("originalTop", ui.position.top).
		        attr("originalLeft", ui.position.left).
		        attr('id', 'deckElement' + $handElement.attr('elementId')).
		        attr('elementId', $handElement.attr('elementId')).
		        draggable().
		        droppable({ greedy: true, drop: $.elements.element_drop }).
		        appendTo($($.elements.deck));
    }

    $.elements.createHandElement = function (id, name)
    {
        var html = '<div class="element" id="a' + id + '" elementId="' + id + '">' + name + '</div>';

        return $(html).
					appendTo($.elements.hand).
					draggable({
					    helper: 'clone'
					});
    }

    $.elements.element_drop = function (event, ui)
    {
        var $element = $(ui.draggable);

        var e1 = $element.attr('elementId');

        var e2 = $(this).attr('elementId');

        var query = "elements[]=" + e1 + "&elements[]=" + e2;

        var $target = $(this);

        $.post("./bus/combine_elements.php", query, function (data)
        {
            if (data.sucess)
            {
                $.elements.combineElements($element, $target, ui, data.Elements);
            }
            else
            {
                if ($element.parent().attr("id") == $.elements.hand.id)
                    alert(1);

                $element.animate(
			        {
			            left: $element.attr("originalLeft"),
			            top: $element.attr("originalTop")
			        }, 'normal');

                $.news.suggest.set($element, $target);

            }
        });
    }

    $.elements.deck_drop = function (event, ui)
    {
        var $element = $(ui.draggable);

        if ($element.parent().attr("id") == $.elements.hand.id)
            $.elements.createDeckElement($element, ui);
    }

    $.elements.body_drop = function (event, ui)
    {
        var $item = $(ui.draggable);

        if ($item.parent().attr('id') == $.elements.deck.id)
            $item.remove();
        else
            $item.draggable({ revert: true });
    }

    $.elements.clear = function ()
    {
        $(".element", $.elements.deck).remove();

        $(".element", $.elements.hand).remove();
    }



})(jQuery);
